﻿using Adnc.Application.Shared;
using Adnc.Application.Shared.Dtos;
using Adnc.WebApi.Shared;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using WYT.Usr.Application.Contracts.Dtos.Wallet;
using WYT.Usr.Application.Contracts.Services;

namespace WYT.Usr.WebApi.Controllers
{
    /// <summary>
    /// 用户钱包
    /// </summary>
    [Route("usr/[Controller]")]
    [ApiController]
    public class WalletController : AdncControllerBase
    {
        private readonly IWalletAppService _walletAppService;
        private readonly IUserContext _userContext;

        public WalletController(
            IWalletAppService walletAppService
            ,IUserContext userContext)
        {
            _walletAppService = walletAppService;
            _userContext = userContext;
        }

        /// <summary>
        /// 获取用户钱包
        /// </summary>
        /// <returns></returns>
        [HttpGet("wallet")]
        public async Task<WYTActionResult<WalletDto>> GetUserWallet()
        {
            return Success(await _walletAppService.GetUserWallet(_userContext.Id));
        }

        /// <summary>
        /// 获取用户钱包日志
        /// </summary>
        /// <param name="input">Value=1:余额日志；=2：购物金日志</param>
        /// <returns></returns>
        [HttpGet("logs")]
         public async Task<WYTActionResult<PageModelDto<WalletLogListDto>>> GetWalletLogs([FromQuery]WalletLogSearchPagedDto input)
        { 
            return Success(await _walletAppService.GetWalletLogs(_userContext.Id, input));
        }

        /// <summary>
        /// 获取收益
        /// </summary>
        /// <param name="start">开始时间</param>
        /// <param name="end">结束时间</param>
        /// <returns></returns>
        [HttpGet("Income")]
        public async Task<WYTActionResult<UseIncomeDto>> GetIncomeInfoAsync([FromQuery]DateTime start, [FromQuery]DateTime end)
        {
            return Success(await _walletAppService.GetIncomeInfoAsync(_userContext.Id, start, end));
        }
        /// <summary>
        /// 用户提现
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("withdrawal")]
        public async Task<WYTActionResult<bool>> UserWithdrawalApplication(WithdrawalDto input)
        {
            return Success(await _walletAppService.UserWithdrawalApplication(_userContext.Id,input));
        }

        /// <summary>
        /// 转账
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("transferAccounts")]
        public async Task<WYTActionResult<bool>> TransferAccounts(WalletTransferAccountsDto input)
        {
            if (input.Money <= 0)
                return new WYTActionResult<bool> { Code = 400, Data = false, Msg = "转账金额错误" };
            if (string.IsNullOrEmpty(input.Pass) || input.Pass.Length != 6 || !Regex.IsMatch(input.Pass, @"^\d+$"))
                return new WYTActionResult<bool>(400,"密码格式错误",false);
            return Success(await _walletAppService.TransferAccounts(_userContext.Id, input));
        }

        #region 内部接口
        /// <summary>
        /// 钱包支付
        /// </summary>
        /// <returns></returns>
        [HttpPost("_pay")]
        [AllowAnonymous]
        public async Task<ActionResult<bool>> WalletPayment(WalletPaymentDto input)
        {
            return Result(await _walletAppService.WalletPayment(input));
        }

        /// <summary>
        /// 获取用户钱包
        /// </summary>
        /// <returns></returns>
        [HttpGet("_wallet")]
        public async Task<ActionResult<WalletDto>> GetRPCUserWallet()
        {
            return Result(await _walletAppService.GetUserWallet(_userContext.Id));
        }
        #endregion
    }
}
